package models;

import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import play.db.jpa.Model;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

@Entity
public class MenuItem extends Model {
	@Column(nullable = false)
	private String name;

	@Column(nullable = true)
	private String description;

	@Column(nullable = false)
	private float price;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "section_id", insertable = false, updatable = false)
	private MenuSection section;

	@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY, mappedBy = "item")
	private Set<OrderItem> orderItems;

	@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
	private Set<Picture> pictures = new HashSet<Picture>();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public MenuSection getSection() {
		return section;
	}

	public void setSection(MenuSection section) {
		this.section = section;
	}

	public Picture getPicture() {
		if (getPictures() != null && getPictures().iterator().hasNext()) {
			return getPictures().iterator().next();
		}
		return null;
	}

	public Set<Picture> getPictures() {
		return pictures;
	}

	public void setPictures(Set<Picture> pictures) {
		this.pictures = pictures;
	}

	public Set<OrderItem> getOrderItems() {
		return orderItems;
	}

	public void setOrderItems(Set<OrderItem> orderItems) {
		this.orderItems = orderItems;
	}

	public static class ItemsJsonSerializer implements JsonSerializer<List<MenuItem>> {
		@Override
		public JsonElement serialize(List<MenuItem> items, Type type, JsonSerializationContext context) {

			JsonArray itemsJson = new JsonArray();

			ItemJsonSerializer itemSerializer = new ItemJsonSerializer();

			for (MenuItem item : items) {
				itemsJson.add(itemSerializer.serialize(item, type, context));
			}

			return itemsJson;
		}
	}

	public static class ItemJsonSerializer implements JsonSerializer<MenuItem> {
		@Override
		public JsonElement serialize(MenuItem item, Type type, JsonSerializationContext context) {
			JsonObject itemJson = new JsonObject();

			itemJson.addProperty("id", item.getId());
			itemJson.addProperty("name", item.getName());
			itemJson.addProperty("price", item.getId());

			return itemJson;
		}
	}
}
